package com.example.zqh.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.zqh.mapper.EmpMapper;
import com.example.zqh.model.RespBean;
import com.example.zqh.pojo.Dept;
import com.example.zqh.mapper.DeptMapper;
import com.example.zqh.pojo.Emp;
import com.example.zqh.pojo.params.OneStringOneNumber;
import com.example.zqh.service.DeptService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.zqh.utils.AssertUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zqh
 * @since 2022-09-10
 */
@Service
public class DeptServiceImpl extends ServiceImpl<DeptMapper, Dept> implements DeptService {

    @Autowired
    DeptMapper deptMapper;

    @Autowired
    EmpMapper empMapper;




    @Override
    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = Exception.class)
    public RespBean departmentAddInterface(String name, String sc) {
        //判断部门名是否重复
        Dept dept = deptMapper.selectOne(new QueryWrapper<Dept>().eq("name", name));
        AssertUtil.isTrue(dept != null,"该部门已存在");
        Dept dept1 = new Dept();
        dept1.setName(name)
                .setIntro(sc);
        int insert = deptMapper.insert(dept1);
        AssertUtil.isTrue(insert != 1, "部门添加失败");
        return RespBean.success("部门添加成功");
    }


    /**
      * @Author: zqh
      * @Date:
      * @Description: 部门删除接口
    */
    @Override
    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = Exception.class)
    public RespBean departmentDeleteInterface(Integer id) {
        //把属于该部门的员工的部门id字段归为0，并把部长的部长字段变为0
        List<Emp> emps = empMapper.selectList(new QueryWrapper<Emp>().eq("dept_id", id));
        for (Emp emp : emps) {
            emp.setDeptId(0);
            emp.setMonitor(0);
            int i = empMapper.updateById(emp);
            AssertUtil.isTrue(i != 1, "部门删除失败");
        }
        int i = deptMapper.deleteById(id);
        AssertUtil.isTrue(i != 1, "部门删除失败");
        return RespBean.success("部门删除成功");
    }

    /**
      * @Author: zqh
      * @Date:
      * @Description: 修改部门的名字
    */
    @Override
    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = Exception.class)
    public RespBean changeTheNameOfTheDepartment(OneStringOneNumber oneStringOneNumber) {
        //判断该名字是否存在
        Dept dept = deptMapper.selectOne(new QueryWrapper<Dept>().eq("name", oneStringOneNumber.getText()));
        AssertUtil.isTrue(dept != null,"该部门已存在");

        Dept dept1 = deptMapper.selectById(oneStringOneNumber.getId());
        dept1.setName(oneStringOneNumber.getText());

        int i = deptMapper.updateById(dept1);
        AssertUtil.isTrue(i != 1,"修改部门名失败");
        return RespBean.success("修改部门名成功");
    }


    /**
      * @Author: zqh
      * @Date:
      * @Description: 修改部门描述
    */
    @Override
    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = Exception.class)
    public RespBean modifyDepartmentDescription(OneStringOneNumber oneStringOneNumber) {
        Dept dept = deptMapper.selectById(oneStringOneNumber.getId());
        dept.setIntro(oneStringOneNumber.getText());
        int i = deptMapper.updateById(dept);
        AssertUtil.isTrue(i != 1, "部门描述修改失败");
        return RespBean.success("修改部门描述成功");
    }


    /**
      * @Author: zqh
      * @Date:
      * @Description: 获取所有部门的数量
    */
    @Override
    @Transactional(readOnly = true)
    public RespBean getTheCountOfAllDepartments() {
        Integer number = deptMapper.getDepthNumber();
        return RespBean.success("查询成功",number);
    }


    /**
      * @Author: zqh
      * @Date:
      * @Description: 分页查询所有部门
    */
    @Override
    @Transactional(readOnly=true)
    public RespBean paginationToQueryAllDepartments(Integer page, Integer size) {
        List<Dept> depts = deptMapper.pagingQueryDepartment((page - 1) * size, size);
        return RespBean.success("查询成功",depts);
    }
}
